home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-1.iso / Files / Internet / Misc / Uupc 3.1 sources.sit / uupc 3.1 sources Folder / Mac specific / ulib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-01  |  8.2 KB  |  416 lines  |  [TEXT/KAHL]

  1. /*        ulib.c
  2.  
  3.  
  4.         macintosh library
  5.         
  6.  
  7.     Things to do in uu host
  8.  
  9.         serial I/O
  10.         
  11.         directory stuff
  12.             opendir, readdir, closedir
  13.  
  14.         prolog and epilog
  15.  
  16.         system call
  17.  
  18.             Portions Copyright © David Platt, 1992, 1991.  All Rights Reserved
  19.             Worldwide.
  20.  
  21. */
  22.  
  23. #include <stdio.h>
  24. #include "dcp.h"
  25. #ifdef     THINK_C
  26. #   include <unix.h>
  27. #else
  28. #    include <sgtty.h>
  29. #endif
  30.  
  31. #include "ulib.proto.h"
  32.  
  33. /*
  34.  *
  35.  *      login (for slave in SLAVELOG mode)
  36.  * Real dumb login handshake.
  37.  *     waitforever = TRUE if an unlimited number of login attempts should be
  38.  *                   permitted (slave mode), and FALSE if the login process
  39.  *                   should terminate after a few attempts (auto mode).
  40.  *     callup_done = TRUE if some form of inbound-call preparation has been done
  41.  *                   (either via the HAYES dialer or via a DIR chat-script) and
  42.  *                   we have some confidence that there's really a caller out there.
  43.  *                   FALSE if we have no such confidence (usually because there was
  44.  *                   no INBOUND entry in the Systems file, and we've simply opened
  45.  *                   the serial port) and we want to require a carriage return to
  46.  *                   elicit the login prompt.
  47. */
  48.  
  49. login(int waitforever, int callup_done) {
  50.     char logmsg[132];
  51.     int len;
  52.     int retries;
  53.     int passTries;
  54. #ifdef SLAVELOG
  55.     msgtime = 1;
  56.     retries = 5;
  57.     while (callup_done && --retries >= 0 && rmsg(logmsg, 0) != 0) {
  58.         ; /* flush any dross in the input queue */
  59.     }
  60.     retries = 10;
  61.     do {
  62.         msgtime = 2 * MSGTIME;
  63.         if (!callup_done) {
  64.             if (0 == rmsg(logmsg, 0)) goto next;
  65.         }
  66.         sprintf(logmsg, "\r\n\n%s uupc listener\r\n\nUsername: ", nodename);
  67.         wmsg(logmsg, 0);
  68.         if (0 == rmsg(logmsg, 0)) goto next;
  69.         if (strstr(logmsg, "Username:") || strstr(logmsg, "Password:") || strstr(logmsg, "NO CARRIER")) {
  70.             printmsg(0, "Modem hung up");
  71.             retries = 0;
  72.             goto next;
  73.         }
  74.         printmsg( 0, "Username = %s", logmsg );
  75.         passTries = 2;
  76. getPassword:
  77.         wmsg("\r\nPassword: ", 0);
  78.         if (0 == rmsg(logmsg, 0)) {
  79.             if (--passTries > 0) goto getPassword;
  80.             goto next;
  81.         }
  82.         printmsg( 14, "\r\nPassword = %s", logmsg );
  83.         if(strcmp(logmsg, password) == EQUAL) 
  84.             return 'I';
  85.         wmsg("\r\nSorry, incorrect login\r\n", 0);
  86. next: ;
  87.     } while ((waitforever || --retries > 0) && Main_State == Call_Systems);
  88. #endif SLAVELOG
  89.     return('A');
  90. }
  91.  
  92.  
  93. char *inbuf;
  94. char *outbuf;
  95.  
  96. swrite(char *data, int num) {
  97.  
  98.     int test;
  99.     unsigned char * cp;
  100.  
  101.     if (debuglevel > 14)
  102.         fputc( '{', stderr );
  103.     if (debuglevel > 14) {
  104.         test = num;
  105.         cp = (unsigned char *)data;
  106.         while (test--)
  107.             fprintf( stderr, isprint(*cp)? "{%c}":"{%02x}", *cp++ );
  108.     }
  109.     test = (*currentConnection->Write)( data, num );
  110.     if (debuglevel > 14)
  111.         fputc( '}', stderr );
  112.     return( test );
  113.  
  114. }
  115.  
  116. void makenull(int refno)
  117. {
  118.     lseek(refno, 0L, SEEK_SET);
  119.     SetEOF(refno, 0L);
  120. }
  121.  
  122. /* non-blocking read essential to "g" protocol */
  123. /* see "dcpgpkt.c" for description */
  124. /* This all changes in a mtask systems. Requests for */
  125. /* I/O should get qued and an event flag given. Then the */
  126. /* requesting process (e.g.gmachine()) waits for the event */
  127. /* flag to fire processing either a read or a write. */
  128. /* Could be implemented on VAX/VMS or DG but not MS-DOS */
  129. sread (char *buf, int num, long int timeout) {
  130. /*
  131.     return( SIORead( buf, num, num, timeout*10 ) ); OBSOLETE
  132. */
  133.     int count;
  134.     int test;
  135.     unsigned char * cp;
  136.  
  137.     if (debuglevel > 13)
  138.         fputc( '[', stderr );
  139.     printmsg( 15, "sread: num: %d  timeout: %d", num, timeout );
  140.         
  141.     count = (*currentConnection->Read) ( buf, num, num, timeout*10 );
  142.     printmsg( 15, "sread: read: %d ", count );
  143.  
  144.     if (debuglevel > 13 && count > 0) {
  145.         test = count;
  146.         cp = (unsigned char *)buf;
  147.         while (test--)
  148.             fprintf( stderr, isprint(*cp)? "[%c]":"[%02x]", *cp++ );
  149.     }
  150.         
  151.     if (debuglevel > 13)
  152.         fputc( ']', stderr );
  153.     return( count );    
  154.     
  155. }
  156.  
  157. /* check number of chars in input buffer */
  158.  
  159. int savail(void)
  160. {
  161.     return (*currentConnection->Avail)();
  162. }
  163.  
  164.  
  165.  
  166. openline(char *name, char *baud, char *phone) {
  167.     int oops;
  168.     printmsg(3, "openline: name: \"%s\"  baud: \"%s\"", name, baud);
  169.     if (strncmp(name, "CTB/", 4) == 0) {
  170.         oops = CTBIOInit(name+4, baud, phone);
  171.     } else {
  172.         oops = SIOInit(name, baud, phone); /* Yes, call SIO directly, currentConnection not yet set up */
  173.     }
  174.     if (oops) {
  175.         return oops;
  176.     }
  177.     inbuf = NewPtr(PORTBUFSIZ);
  178.     if (inbuf) {
  179.         (*currentConnection->InBuffer)(inbuf, PORTBUFSIZ);
  180.     }
  181.     outbuf = NewPtr(PORTBUFSIZ);
  182.     if (outbuf) {
  183.         (*currentConnection->OutBuffer)(outbuf, PORTBUFSIZ);
  184.     }
  185.     return(0);
  186. }
  187.  
  188.  
  189. closeline(void)
  190. {
  191.     hangup();
  192.     if (currentConnection) {
  193.         (*currentConnection->Close) ( 1 );
  194.     }
  195.     if (inbuf) {
  196.         DisposPtr(inbuf);
  197.         inbuf = NULL;
  198.     }
  199.     if (outbuf) {
  200.         DisposPtr(outbuf);
  201.         outbuf = NULL;
  202.     }
  203. }
  204.  
  205. flowcontrol(int software, int hardware)
  206. {
  207.     (*currentConnection->SetFlowCtl) (software, hardware);
  208. }
  209.  
  210. nodot(int string)
  211. {
  212. }
  213.  
  214.  
  215.  
  216. notimp(int argc, char *argv[])
  217. {
  218.     /*debuglevelMsg("\Pcheck argc (08) and argv (0a) ");*/
  219.     fprintf( stderr, "shell: %s not implemented\n", *argv );
  220. }
  221.  
  222. /*         shell
  223.  
  224.  
  225. */
  226.  
  227. char *getcwd();
  228.  
  229. shell(char *command, char *inname, char *outname) {
  230.  
  231.     char *argvec[50];
  232.     int is_ok;
  233.  
  234.     int rnews();
  235.     int macbin();
  236.     
  237.     int argcp;
  238.  
  239.     char **argvp;
  240.     char args;
  241.     char *openFlags;
  242.     
  243.  
  244.     int    (*proto)();
  245.     
  246.     if (strlen(command) == 0) {
  247.         printmsg(0, "Missing or null command in X. file, no action taken");
  248.         return 1;
  249.     }
  250.     
  251.     argcp = 0;
  252.  
  253.     argcp = getargs( command, argvec );
  254.  
  255.     argvp = argvec;
  256.     args = argcp;
  257.  
  258.     if ( debuglevel > 5 ) {
  259.         while ( args ) 
  260.             fprintf( stderr, "arg: %d  %s\n", args--, *argvp++ );
  261.         argvp = argvec;
  262.         args = argcp;
  263.     }
  264.     /* */
  265.     
  266.     proto = notimp;
  267.     openFlags = "r";
  268.  
  269.     if ( strcmp( *argvp, "rmail" ) == SAME )
  270.         proto = rmail;
  271.  
  272.     else if ( strcmp( *argvp, "rnews" ) == SAME ) {
  273.         openFlags = "rb";
  274.         proto = rnews;
  275.     }
  276.     
  277.     else if ( strcmp( *argvp, "macbin" ) == SAME ) {
  278.         openFlags = "rb";
  279.         proto = macbin;
  280.     }
  281.  
  282.     is_ok = 1;
  283.         
  284.     if ( *inname != '\0' ) {
  285.         /* fprintf( stderr, "reopening stdin as %s\n", inname );
  286.         fprintf( stderr, "curdir: %s\n", getcwd(NULL, 0)); /* */
  287.         mapMacCaseness(inname);
  288.         if ( freopen( inname, openFlags, stdin ) == NULL ) {
  289.             fprintf( stderr, "Couldn't open %s, %d\n", inname, errno );
  290.             is_ok = 0;
  291.         }
  292.     }
  293.     if (is_ok) {
  294.         is_ok = (*proto)( argcp, argvp );
  295.     }
  296.  
  297.     if (freopen( ".console", "r", stdin ) == NULL) {;
  298.         fprintf( stderr, "Couldn't reopen console, %d\n", errno );
  299.     }
  300.     return is_ok;
  301. }
  302.  
  303.  
  304. /*
  305.     macbin
  306.  
  307.     copy a macbinary image from stdin into a macintosh file
  308.  
  309.         128 bytes of header information
  310.         (n+128)/128*128 bytes of data fork data
  311.         (n+128)/128*128 bytes of resource fork data
  312.  
  313.     The name is derived from the header information
  314.  
  315.     The directory is given as an arguement
  316.  
  317.  
  318.     on remote system use:
  319.  
  320.         uux macfile.mb > remotemac!macbin /usr/sl
  321.  
  322.  
  323. */
  324.  
  325. #undef TRUE
  326.  
  327. #ifndef    THINK_C
  328. # include <types.h>
  329. # include <pb.h>
  330. #endif
  331.  
  332. /* #include <stdio.h> */
  333.  
  334. typedef struct {
  335.     /*
  336.     char                blknum;
  337.     char                mblknum;
  338.     */
  339.     char                dummy;
  340.  
  341.     char                version;
  342.  
  343.     char                fName[64];
  344.     FInfo                finderInfo;            /* check - starts on odd byte boundary */
  345.     char                protected;
  346.     char                ckZero1;
  347.     long                dfLength;
  348.     long                rfLength;
  349.     long                crDat;
  350.     long                mdDat;
  351.     char                fill1[27];
  352.     char                computer;
  353.     char                os;
  354.     } MacBinHeader;
  355.  
  356.  
  357.  
  358. int macbin(int argc, char *argv[])
  359. {
  360. #ifdef TESTETEST
  361.     char    filename[132];
  362.     char    buf[BUFSIZ];
  363.     MacBinHeader mbh;
  364.     long    count, size;
  365.     int        i;
  366.  
  367.     HPrmBlkRec    pbr;
  368.  
  369.     /* get the header */
  370.     fread( &mbh.version, 1, 128, stdin );
  371.  
  372.  
  373.     /* create and open the file */
  374.     strncpy( pbr.ioNamePtr, mbh.fName, 64 );
  375.     pbr.ioVRefNum = 0;
  376.     pbr.u.hfp.ioFVersNum = 0;
  377.     PBCreate( &pbr, FALSE );
  378.  
  379.  
  380.     /* get finder info */
  381.     pbr.u.hfp.ioFDirIndex = 0;
  382.     pbr.u.hfp.ioDirID = 0L;
  383.     PBGetInfo( &pbr, FALSE );
  384.     
  385.     /* set finder info */
  386.     pbr.u.hfp.ioFlFndrInfo.fdFldr = 0;
  387.     pbr.u.hfp.ioFlCrDat = mbh.crDat;
  388.     pbr.u.hfp.ioFlMdDat = mbh.mdDat;
  389.     pbr.u.hfp.ioFlFndrInfo = mbh.finderInfo;
  390.     PBSetInfo( &pbr, FALSE );
  391.  
  392.     /* get and write out the fork */
  393.     pbr.u.iop.ioPermssn = 0;
  394.     pbr.u.iop.ioMisc = 0L;
  395.     for ( i = 1; i > 0 ; i-- ) {
  396.         if (i == 1 ) {
  397.             PBOpen( &pbr, FALSE );
  398.             size = (mbh.dfLength + 127) & 0xffffff80;
  399.         }
  400.         else {
  401.             PBROpen( &pbr, FALSE );
  402.             size = (mbh.rfLength + 127) & 0xffffff80;
  403.         }
  404.         while ( size > 0 )
  405.             if ( (count = fread( buf, 1, BUFSIZ, stdin )) != 0 ) {
  406.                 size -= count;
  407.                 pbr.u.iop.ioBuffer = buf;
  408.                 pbr.u.iop.ioReqCount = count;
  409.                 PBWrite ( &pbr, FALSE);
  410.             }
  411.         PBClose( &pbr, FALSE );
  412.     }
  413. #endif
  414.     return 1;
  415. }
  416.